From: Andrew Cooper Date: Thu, 2 Mar 2017 18:36:54 +0000 (+0000) Subject: tools/insn-fuzz: Don't hit memcpy() for zero-length reads X-Git-Tag: archive/raspbian/4.11.1-1+rpi1~1^2~66^2~2308 X-Git-Url: https://dgit.raspbian.org/%22http:/www.example.com/cgi/%22https:/%22bookmarks://%22Dat/%22http:/www.example.com/cgi/%22https:/%22bookmarks:/%22Dat?a=commitdiff_plain;h=654740b4bd8dfb358a9cf6876e60b79395a1d1fb;p=xen.git tools/insn-fuzz: Don't hit memcpy() for zero-length reads For control-flow changes, the emulator needs to perform a zero-length instruction fetch at the target offset. It also passes NULL for the destination buffer, as there is no instruction stream to collect. This trips up UBSAN when passed to memcpy(), as passing NULL is undefined behaviour per the C spec (irrespective of passing a size of 0). Special case these fetches in fuzz_insn_fetch() before reaching data_read(). Signed-off-by: Andrew Cooper Acked-by: George Dunlap Reviewed-by: Jan Beulich --- diff --git a/tools/fuzz/x86_instruction_emulator/fuzz-emul.c b/tools/fuzz/x86_instruction_emulator/fuzz-emul.c index 65c5a3bcf3..64b7fb230e 100644 --- a/tools/fuzz/x86_instruction_emulator/fuzz-emul.c +++ b/tools/fuzz/x86_instruction_emulator/fuzz-emul.c @@ -117,6 +117,16 @@ static int fuzz_insn_fetch( unsigned int bytes, struct x86_emulate_ctxt *ctxt) { + /* + * Zero-length instruction fetches are made at the destination of jumps, + * to perform segmentation checks. No data needs returning. + */ + if ( bytes == 0 ) + { + assert(p_data == NULL); + return maybe_fail("insn_fetch", true); + } + return data_read("insn_fetch", p_data, bytes); }